package cn.zbh.system.controller;

import cn.zbh.basic.util.AjaxResult;
import cn.zbh.basic.util.PageList;
import cn.zbh.system.domain.Systemdictionarydetail;
import cn.zbh.system.query.SystemdictionarydetailQuery;
import cn.zbh.system.service.ISystemdictionarydetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/systemdictionarydetail")
@Api(value = "各种类型的查询",description="各种类型相关的CRUD功能")
public class SystemdictionarydetailController {

    @Autowired
    private ISystemdictionarydetailService systemdictionarydetailService;
    @GetMapping
    @ApiOperation(value = "通过ID查询所有" )
    public List<Systemdictionarydetail> findAll(){
        return systemdictionarydetailService.findAll();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Systemdictionarydetail findById(@PathVariable("id") Long id){
        return systemdictionarydetailService.findById(id);
    }

    @PostMapping
    @ApiOperation(value = "分页" )
    public PageList<Systemdictionarydetail> query(@RequestBody SystemdictionarydetailQuery query){
        try {
            return systemdictionarydetailService.query(query);
        } catch (Exception e) {
            e.printStackTrace();
            return  new PageList<Systemdictionarydetail>();
        }

    }


    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            systemdictionarydetailService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMsg("操作失败");
        }
    }

    @PutMapping
    @ApiOperation(value = "添加或修改" )
    public AjaxResult updateOrAdd(@RequestBody Systemdictionarydetail Systemdictionarydetail) {
        try {
            if (Systemdictionarydetail.getId() == null) {
                systemdictionarydetailService.add(Systemdictionarydetail);
            } else {
                systemdictionarydetailService.update(Systemdictionarydetail);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "操作失败");
        }
    }



   @PatchMapping
    @ApiOperation(value = "批量删除" )
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            systemdictionarydetailService.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setMsg("操作失败");
        }
    }

    /**
     *  根据types_ID查询数据数据字典明细
     */
    @GetMapping("/{types_id}")
    public List<Systemdictionarydetail> loadByTypeId(@PathVariable("types_id") Long types_id) {
        return systemdictionarydetailService.loadByTypeId(types_id);
    }

}
